package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.exception.WisdomFactoryException;
import com.igeek.boot.mapper.AssetsMapper;
import com.igeek.boot.service.IAssetsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.vo.PageQueryVO;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-22
 */
@Service
public class AssetsServiceImpl extends ServiceImpl<AssetsMapper, Assets> implements IAssetsService {

    //查询物资的所有信息
    @Override
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "assets", keyGenerator = "keyGenerator")
    public List<Assets> findAll() {
        //查询物资表的信息
        List<Assets> assets = baseMapper.selectList(null);
        //模拟异常信息
        //if(assets.size() >= 0){
        //    throw new WisdomFactoryException("模拟运行时异常成功");
        //};
        return assets;
    }

    //根据id查询物资信息
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "assets", keyGenerator = "keyGenerator")
    public Assets findOneById(Integer id){
        return baseMapper.selectById(id);
    }

    //分页查询
    @Override
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "assets", keyGenerator = "keyGenerator")
    public Page<Assets> findPage(PageQueryVO pageQueryVO) {
        if(ObjectUtils.isEmpty(pageQueryVO)){
            throw new WisdomFactoryException("分页查询参数不能为空");
        }
        //获取分页的条件
        Integer currentPage = pageQueryVO.getCurrentPage();
        Integer pageSize = pageQueryVO.getPageSize();
        Page<Assets> page = new Page<>(currentPage , pageSize);

        //封装条件
        String queryString = pageQueryVO.getQueryString();
        LambdaQueryWrapper<Assets> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.or().eq(StringUtils.hasLength(queryString),Assets::getAssetID,queryString);
        lambdaQueryWrapper.or().like(StringUtils.hasLength(queryString),Assets::getAssetName,queryString);

        //执行查询
        Page<Assets> resultPage = baseMapper.selectPage(page, lambdaQueryWrapper);
        return resultPage;
    }

    /**
     * 添加物资信息
     * @param assets
     * @return
     */
    @Override
    @Transactional
    public boolean add(Assets assets) {
        //获取添加的条件
        if(ObjectUtils.isEmpty(assets)){
            throw new WisdomFactoryException("物资表添加参数不能为空");
        }
        int insert = baseMapper.insert(assets);
        return insert>0;
    }

    //根据货架id查询所有的物资信息
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "assets", keyGenerator = "keyGenerator")
    @Override
    public List<Assets> findAllByShelfId(Integer id) {
        new LambdaQueryWrapper<Assets>().eq(Assets::getShelfId,id);
        return baseMapper.selectList(
                new LambdaQueryWrapper<Assets>().eq(!ObjectUtils.isEmpty(id),Assets::getShelfId, id));
    }

    //1.根据货架id查询物资信息并在物资信息表中寻找和当前入库的物资名和类型一致的物资
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "assets", keyGenerator = "keyGenerator")
    @Override
    public Assets selectByShelfIdAndNameType(Integer shelfID, String incomingName, String incomingType) {
        Assets assets = baseMapper.selectOne(new LambdaQueryWrapper<Assets>()
                .eq(!ObjectUtils.isEmpty(shelfID), Assets::getShelfId, shelfID)
                .eq(StringUtils.hasLength(incomingName), Assets::getAssetName, incomingName)
                .eq(StringUtils.hasLength(incomingType), Assets::getAssetType, incomingType));
        if(!ObjectUtils.isEmpty(assets)){
            return assets;
        }
        return null;
    }

}
